// pages/my/merchandise/history/history.js
import CommonPage from "../../common/CommonPage.js"
const app = getApp()
const api = require('../../api/http.js')
const config = require('../../../config.js')

class HistoryPage extends CommonPage {

  /**
   * 页面的初始数据
   */
  constructor(...args) {
    super(...args)
    super.$init({
      historyList: [],
      showImage: 'true',
      height: "0rpx",
      backImagePath: app.globalData.backImagePath,
      titleName: "兑换记录"
    })
  }
  initData = function(query, resovle, reject) {
    var that = this
    that.data.height = app.globalData.titleHeight
    var param = {
      openId: query.openId
    }
    api.post(config.exchangeListOfUserUrl, param).then(res => {
      console.debug("兑换记录", res)
      that.data.historyList = res
      that.$setData(that.data)  //原先方式，在CommonPage失效时兼容
      // this.$resolve(that.data)  //当使用异步请求时
    })
  }
  $onNavigator(query = {}) {
    console.debug("预加载兑换记录；参数：",query)
    this.$put("history-data", this.initData.bind(this), query)
  }
  back = function() {
    app.back()
  }
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad = function(options) {
    this.setData({
      height: app.globalData.titleHeight
    })
    const navData = this.$take("history-data")
    if (navData) {
      return
      // console.debug("预加载兑换记录数据：", navData)
      // navData.then(res => {
      //   //必须initData时调用resolve才能解析
      //   console.log("Result", res)
      // })
    } else {
      console.debug("普通加载兑换记录数据")
      this.initData()
    }
  }
}
Page(new HistoryPage({clazzName: "HistoryPage"}))
// Page(new HistoryPage())

